package com.broada.apm.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.broada.apm.ajax.Result;
import com.broada.apm.ajax.ServerResult;
import com.broada.apm.entity.Service;
import com.broada.apm.entity.ServiceLua;
import com.broada.apm.entity.ServiceProtocol;
import com.broada.apm.model.LuaSettings;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

@Path("apm/nodes")
@Api(value="apm/nodes" , description = "节点相关的APIs")
@Produces(MediaType.APPLICATION_JSON)
public interface ServerService {
	/**
	 * 查询除去topology中节点后的节点信息
	 * direction取值为last、next和空值，为last则查询上级节点，为next，则查询下级节点,空值则根据关键字查询
	 * 
	 * @return
	 */
	@GET
	@ApiOperation(value = "查询除去topology中节点后的节点信息,direction取值为last、next和空值，为last则查询上级节点，为next，则查询下级节点,空值则根据关键字查询")
	@Produces(MediaType.APPLICATION_JSON)
	public ServerResult queryDiscoveryServices(
			@ApiParam(value = "关键词", required = false) @QueryParam("keyWord") String keyWord,
			@ApiParam(value = "节点id", required = true) @QueryParam("serviceId") String serviceId, 
			@ApiParam(value = "last、next和空值，为last则查询上级节点，为next，则查询下级节点", required = false) @QueryParam("direction") String direction,
			@ApiParam(value = "应用系统id", required = true) @QueryParam("appId") String appId, 
			@ApiParam(value = "每页显示的查询记录数", required = true) @QueryParam("pageSize") int pageSize,
			@ApiParam(value = "页码", required = true) @QueryParam("pageIndex") int pageIndex, 
			@ApiParam(value = "排序字段", required = false) @QueryParam("orderBy") String orderBy, 
			@ApiParam(value = "排序方向，true为降序", required = false) @QueryParam("desc")  boolean desc);

	/**
	 * 根据节点id获取单个节点信息
	 * 
	 * @return
	 */
	@GET
	@Path("/{nodeId}")
	@ApiOperation(value = "根据节点id获取单个节点信息")
	@Produces(MediaType.APPLICATION_JSON)
	public ServerResult queryServiceById(@ApiParam(value = "节点id", required = true) @PathParam("nodeId") String nodeId);

	/**
	 * 删除节点
	 * 
	 * @param nodeId
	 */
	@DELETE
	@Path("/{nodeId}")
	@ApiOperation(value = "删除节点")
	@Produces(MediaType.APPLICATION_JSON)
	public ServerResult deleteService(@ApiParam(value = "节点id", required = true) @PathParam("nodeId") String nodeId);

	/**
	 * 添加节点
	 * 
	 * @param service
	 */
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "添加节点")
	public ServerResult saveService(@ApiParam(value = "需要添加的节点", required = true) Service service);

	/**
	 * 修改节点名称
	 * 
	 * @param nodeId
	 * @param service
	 */
	@PUT
	@Path("/{nodeId}")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "修改节点名称")
	public ServerResult updateServiceName(
			@ApiParam(value = "节点id", required = true) @PathParam("nodeId") String nodeId, 
			@ApiParam(value = "需要修改的节点", required = true) Service service);
	
	/**
	 * 修改节点协议
	 * 
	 * @param map
	 */
	@PUT
	@Path("/protocol")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "修改节点协议")
	public ServerResult updateServiceProtocol(@ApiParam(value = "需要修改的节点协议", required = true) ServiceProtocol sp);

	/**
	 * 删除dis_service表中手动添加节点
	 * 
	 * @param id
	 * @return
	 */
	@Path("/discovery")
	@DELETE
	@ApiOperation(value = "删除手动添加节点")
	@Produces(MediaType.APPLICATION_JSON)
	public ServerResult deleteDiscoveryService(@ApiParam(value = "节点id", required = true) @QueryParam("nodeIds") String id);

	/**
	 * 生成申请文件
	 * 
	 * @param request
	 * @param response
	 * @param comsumerName
	 * @return
	 * @throws Exception
	 */
	@GET
	@Path("/licreq")
	@ApiOperation(value = "生成申请文件")
	public String licreq(@Context HttpServletRequest request, @Context HttpServletResponse response,
			@ApiParam(value = "客户名称", required = true) @QueryParam("consumer") String consumer) throws Exception;

	/**
	 * 导入lic
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@POST
	@Path("/licimport")
	@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON })
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "导入lic")
	public ServerResult licimport(@Context HttpServletRequest request, @Context HttpServletResponse response);

	/**
	 * 添加Lua脚本
	 * 
	 * @param nodeId
	 * @param luaScript
	 */
	@PUT
	@Path("/lua")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "添加Lua脚本")
	public ServerResult saveLuaScript(@ApiParam(value = "添加的Lua脚本", required = true) ServiceLua serviceLua);

	/**
	 * 保存SSL证书
	 */
	@POST
	@Path("/ssl")
	@Consumes({ MediaType.MULTIPART_FORM_DATA, MediaType.APPLICATION_JSON })
	@Produces(MediaType.TEXT_HTML)
	@ApiOperation(value = "保存ssl证书")
	public String saveSSLCertificate(
			@ApiParam(value = "节点id", required = true) @QueryParam("serviceId") String serviceId,
			@ApiParam(value = "ssl证书类型", required = true) @QueryParam("sslType") String sslType, 
			@ApiParam(value = "ssl证书密码", required = true) @QueryParam("sslPwd") String sslPwd,
			@ApiParam(value = "返回值", required = true) @QueryParam("callback") String callBack, 
			@Context HttpServletRequest request, @Context HttpServletResponse response);

	/**
	 * 抓包
	 * 
	 * @param service
	 */
	@GET
	@Path("/downloadPSP")
	@ApiOperation(value = "抓包")
	public ServerResult packetCapture(
			@ApiParam(value = "节点id", required = true) @QueryParam("serviceId") String serviceId, 
			@ApiParam(value = "抓包周期 单位(s)", required = true) @QueryParam("time") int time);

	/**
	 * 查询节点的SSL信息
	 * 
	 * @param serviceId
	 * @return
	 */
	@GET
	@Path("/ssl")
	@ApiOperation(value = "查询节点的ssl信息")
	@Produces(MediaType.APPLICATION_JSON)
	public ServerResult querySSLByServiceId(@ApiParam(value = "节点id", required = true) @QueryParam("serviceId") String serviceId);

	/**
	 * 删除已设定的SSL证书
	 * 
	 * @param serviceId
	 */
	@DELETE
	@Path("/ssl")
	@ApiOperation(value = "删除已设定的SSL证书")
	@Produces(MediaType.APPLICATION_JSON)
	public ServerResult deleteSSLCertificate(@ApiParam(value = "节点id", required = true) @QueryParam("serviceId") String serviceId);

	/**
	 * 查询节点的lua脚本
	 * 
	 * @param serviceId
	 * @return
	 */
	@GET
	@Path("/lua")
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "查询节点的lua脚本")
	public ServerResult queryLuaScriptByServiceId(@ApiParam(value = "节点id", required = true) @QueryParam("serviceId") String serviceId);
	
	/**
	 * 查询所有服务节点
	 * @param ip
	 * @param serviceType
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@GET
	@Path("/service")
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "服务节点")
	public ServerResult queryService(
			@ApiParam(value = "节点ip", required = false) @QueryParam("ip") String ip, 
			@ApiParam(value = "节点端口", required = false) @QueryParam("port") String port, 
			@ApiParam(value = "节点类型", required = false) @QueryParam("serviceType") String serviceType,
			@ApiParam(value = "是否是已关注节点，1为已关注", required = true) @QueryParam("focus") int focus, 
			@ApiParam(value = "告警等级", required = false) @QueryParam("serverity") int serverity, 
			@ApiParam(value = "查询开始时间", required = true) @QueryParam("startDate") long startDate,
			@ApiParam(value = "查询结束时间", required = true) @QueryParam("endDate") long endDate, 
			@ApiParam(value = "排序字段", required = false) @QueryParam("orderBy") String orderBy, 
			@ApiParam(value = "排序顺序，true为降序", required = false) @QueryParam("desc") boolean desc, 
			@ApiParam(value = "每页显示的查询记录数", required = true) @QueryParam("pageSize") int pageSize,
			@ApiParam(value = "页码", required = true) @QueryParam("pageIndex") int pageIndex);
	
	/**
	 * 关注或取消关注服务节点
	 * @param serviceId
	 * @param service
	 * @return
	 */
	@PUT
	@Path("{serviceId}/service/focus")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "关注或取消关注服务节点")
	public ServerResult serviceFocusOnOrNot(@ApiParam(value = "节点id", required = true) @PathParam("serviceId") String serviceId, Service service);
	
	/**
	 * 统计已关注服务节点数和全部服务节点数
	 * @return
	 */
	@GET
	@Path("/serviceFocusCount")
	@ApiOperation(value = "统计已关注服务节点数和全部服务节点数")
	@Produces(MediaType.APPLICATION_JSON)
	public ServerResult queryServiceFocusCount();
	
	/**
	 * 服务节点命名
	 * 
	 * @param nodeId
	 * @param service
	 */
	@Path("/{nodeId}/serviceName")
	@PUT
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "服务节点命名")
	public ServerResult setServiceName(
			@ApiParam(value = "节点id", required = true) @PathParam("nodeId") String nodeId, 
			@ApiParam(value = "提交的重命名的服务节点", required = true) Service service);
	
	/**
	 * 根据服务节点id查找服务节点
	 * @return
	 */
	@GET
	@Path("/disService")
	@ApiOperation(value = "根据服务节点id查找服务节点")
	public ServerResult queryDisService(@ApiParam(value = "节点id", required = true) @QueryParam("serviceId") String serviceId);
	
	/**
	 * 更新节点
	 * @param nodeId
	 * @param service
	 * @return
	 */
	@Path("/{appId}/{nodeId}/ips")
	@PUT
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "更新节点")
	public ServerResult updateServiceIps(
			@ApiParam(value = "应用系统id", required = true) @PathParam("appId") String appId, 
			@ApiParam(value = "节点id", required = true) @PathParam("nodeId") String nodeId, 
			@ApiParam(value = "需要更新的节点", required = true) Service service);
	
	/**
	 * 保存luaSettings
	 * @return
	 */
	@POST
	@Path("/luaSettings")
	@ApiOperation(value = "保存luaSettings")
	@Produces(MediaType.APPLICATION_JSON)
	public Result saveLuaSettings(@ApiParam(value = "需要保存的luaSettings", required = true) LuaSettings settings);
	
	/**
	 * 查看luaSettings
	 * @return
	 */
	@GET
	@Path("/luaSettings")
	@ApiOperation(value = "查看luaSettings")
	@Produces(MediaType.APPLICATION_JSON)
	public Result queryLuaSettings(@ApiParam(value = "节点id", required = true) @QueryParam("serviceId") String serviceId);
}
